﻿using FangPage.Common;
using FangPage.Core;
using FangPage.Data;
using FangPage.User;
using FangPage.User.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace FangPage.User.Controller
{
    public class depart_tree : UserController
    {
        protected List<Department> departlist = new List<Department>();

        public override void Controller()
        {
            SqlParam depart_sql = DbHelper.MakeOrderBy("display", OrderBy.ASC);

            departlist = DbHelper.ExcuteList<Department>(depart_sql);

            List<FPObject> data = GetDepartChildren("");

            FPResponse.WriteData(data);
        }

        /// <summary>
        /// 获取部门数据
        /// </summary>
        /// <param name="parentid"></param>
        /// <returns></returns>
        private List<FPObject> GetDepartChildren(string parentid)
        {
            List<FPObject> departs_data = new List<FPObject>();

            List<Department> children = departlist.Where(item => item.parentid == parentid).ToList();

            foreach (var item in children)
            {
                FPObject obj = new FPObject();

                obj["title"] = item.name;
                obj["id"] = item.id;
                obj["field"] = item.shortname;
                obj["spread"] = true;

                List<FPObject> departs_children = GetDepartChildren(item.id);

                obj["children"] = departs_children;

                departs_data.Add(obj);
            }

            return departs_data;
        }
    }
}
